const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    albumnav: ["专题", "热播", "推荐", "达人"],
    albumnavcurrent: 0,
    ishot: true,
    isTopic: 1,
    isMust: 0,
    sortBy: 0,
    albumlists1: [],
    albumlists2: [],
    page: 1,
    pageSize: 10,
    isEnd: false,
    title:'专题', 
    isloading:true,
  },
  timer:null,
  onLoad(e) {
    if (e) {
      this.setData({
        isTopic: e.isTopic,
        isMust: e.isMust,
        sortBy: e.sortBy,
        albumnavcurrent: e.albumnavcurrent
      })
      if (e.sortBy==2) {
        this.setData({
          ishot: true
        })
      } else {
        this.setData({
          ishot: false
        })
      }
      wx.setNavigationBarTitle({
        title: this.data.albumnav[e.albumnavcurrent] || '专题'
      })

      this.collectionList()
    }
  },
  albumnav(e) {
    var index = e.currentTarget.dataset.index;
    var text = e.currentTarget.dataset.text;
    this.setData({
      albumnavcurrent: index,
      ishot: true,
      sortBy: 2,
      albumlists1: [],
      albumlists2: [],
      page: 1,
      pageSize: 10,
      isEnd: false,
      isloading:true,
    })
    if (index == 0) {
      this.setData({
        isMust: 0,
        isTopic: 1
      })
    }
    if (index == 2) {
      this.setData({
        isMust: 1,
        isTopic: 0
      })
    }
    wx.setNavigationBarTitle({
      title: this.data.albumnav[this.data.albumnavcurrent] || '专题'
    })
    this.collectionList();
  },
  hot() {
    this.setData({
      ishot: !this.data.ishot,
      albumlists1: [],
      albumlists2: [],
      isloading:true,
      page: 1,
      pageSize: 10,
      isEnd: false,
    })
    if (this.data.ishot) {
      this.setData({
        sortBy: 2
      })
    } else {
      this.setData({
        sortBy: 1
      })
    }
    wx.showLoading({
      title:'请求中~~'
    })
    if(this.timer){
      clearTimeout(this.timer)
    }
    this.timer = setTimeout(()=>{
      this.collectionList();
      wx.hideLoading()
    },500)
  },
  collectionList() {
    if (this.data.albumnavcurrent == 1) {
      //热播
      Controller.audioList({
        sortBy: this.data.sortBy,
        isHotPlay:1,
        page: this.data.page,
        pageSize: this.data.pageSize
      }).then(res => {
        if (res.data.baseData.list.length > 0) {
          this.setData({
            albumlists2: this.data.albumlists2.concat(res.data.baseData.list),
            isEnd:res.data.baseData.lastPage
          })
        }
        this.setData({isloading:false})
      }).catch(res=>{
        this.setData({isloading:false})
      })
    } else if (this.data.albumnavcurrent == 0) {
      // 专题
      Controller.compilationList({
        sortBy: this.data.sortBy,
        isTopic: this.data.isTopic,
        page: this.data.page,
        pageSize: this.data.pageSize
      }).then(res => {
        if (res.data.baseData.list.length > 0) {
          this.setData({
            albumlists1: this.data.albumlists1.concat(res.data.baseData.list),
            isEnd:res.data.baseData.lastPage
          })
        }
        this.setData({isloading:false})
      }).catch(res=>{
        this.setData({isloading:false})
      })
    }else {
      Controller.collectionList({
        sortBy: this.data.sortBy,
        isMust: this.data.isMust,
        page: this.data.page,
        pageSize: this.data.pageSize
      }).then(res => {
        if (res.data.baseData.list.length > 0) {
          this.setData({
            albumlists1: this.data.albumlists1.concat(res.data.baseData.list),
            isEnd:res.data.baseData.lastPage
          })
        }
        this.setData({isloading:false})
      }).catch(res=>{
        this.setData({isloading:false})
      })
    }
  },
  scrollend() {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1,
      });
      this.collectionList()
    }
  },
  onHide(){
    clearTimeout(this.timer)
  },
  mvlook(e){
    wx.navigateTo({
      url: '/pages/weixinlink/weixinlink?url='+e.currentTarget.dataset.url
    })
  },
  mvmin(e){
    wx.navigateToMiniProgram({
      appId: e.currentTarget.dataset.id,
      path: e.currentTarget.dataset.path,
      extraData: {},
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },
  albumgo(e){
    var type=e.currentTarget.dataset.type;
    if(type==1){
      wx.navigateTo({
        url: '/pages/Exclusive/Exclusive?id='+e.currentTarget.dataset.id
      })
    }else{
      wx.navigateTo({
        url: '/pages/Album/Detail/Detail?id='+e.currentTarget.dataset.id +'&sortBy='+this.data.sortBy
      })
    }
    
  },
  playsingle(e) {
    var index = 0;
    if (e.currentTarget.dataset.item) {
      app.globalData.currentIndex = 0
      app.globalData.songlist = [e.currentTarget.dataset.item]
      // this._init()
      wx.navigateTo({
        url: "/pages/Playing/Playing"
      })
    } else {
      wx.showToast({
        title: '本歌曲暂时不能播放。请切换下一首',
        icon: 'none'
      })
    }

  },
  onShareAppMessage: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·专题',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
  onShareTimeline: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·专题',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
})